<!DOCTYPE HTML>
<html lang="en-US" >
    
    <head>
        
        <meta charset="UTF-8">
        <title>SEL类型 | OC中的私有方法</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 1.0.3">
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    
    
    
    
    <link rel="prev" href="../14.html" />
    

        
    </head>
    <body>
        
        
<link rel="stylesheet" href="../gitbook/style.css">


        
    <div class="book"  data-level="13" data-basepath=".." data-revision="1442488483454">
    

<div class="book-summary">
    <div class="book-search">
        <input type="text" placeholder="Type to search" class="form-control" />
    </div>
    <ul class="summary">
        
    	
    	
    	

        

        
    
        
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                        <i class="fa fa-check"></i>
                        
                         OC中的私有方法 
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1" data-path="property/README.html">
            
                
                    <a href="../property/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                         @property基本概念 
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2" data-path="synthesize/README.html">
            
                
                    <a href="../synthesize/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                         @synthesize基本概念
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3" data-path="5.html">
            
                
                    <a href="../5.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                         @property增强
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="4" data-path="6.html">
            
                
                    <a href="../6.html">
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                         @property修饰符
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="5" data-path="id/README.html">
            
                
                    <a href="../id/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                         id类型
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="6" data-path="new/README.html">
            
                
                    <a href="../new/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                         new方法实现原理
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="7" data-path="9.html">
            
                
                    <a href="../9.html">
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                         构造方法
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="8" data-path="10.html">
            
                
                    <a href="../10.html">
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                         自定义构造方法
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="9" data-path="12.html">
            
                
                    <a href="../12.html">
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                         继承中的自定义构造方法
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="10" data-path="13.html">
            
                
                    <a href="../13.html">
                        <i class="fa fa-check"></i>
                        
                            <b>10.</b>
                        
                         自定义类工厂方法
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="11" data-path="1.html">
            
                
                    <a href="../1.html">
                        <i class="fa fa-check"></i>
                        
                            <b>11.</b>
                        
                         类的本质
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="12" data-path="14.html">
            
                
                    <a href="../14.html">
                        <i class="fa fa-check"></i>
                        
                            <b>12.</b>
                        
                         类的启动过程
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter active" data-level="13" data-path="sel/README.html">
            
                
                    <a href="../sel/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>13.</b>
                        
                         SEL类型
                    </a>
                
            
            
        </li>
    


        
        <li class="divider"></li>
        <li>
            <a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Published using GitBook</a>
        </li>
        
    </ul>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header">
    <!-- Actions Left -->
    <a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>
    <a href="#" class="btn pull-left toggle-search" aria-label="Toggle search"><i class="fa fa-search"></i></a>
    
    <div id="font-settings-wrapper" class="dropdown pull-left">
        <a href="#" class="btn toggle-dropdown" aria-label="Toggle font settings"><i class="fa fa-font"></i>
        </a>
        <div class="dropdown-menu font-settings">
    <div class="dropdown-caret">
        <span class="caret-outer"></span>
        <span class="caret-inner"></span>
    </div>

    <div class="buttons">
        <button type="button" id="reduce-font-size" class="button size-2">A</button>
        <button type="button" id="enlarge-font-size" class="button size-2">A</button>
    </div>

    <div class="buttons font-family-list">
        <button type="button" data-font="0" class="button">Serif</button>
        <button type="button" data-font="1" class="button">Sans</button>
    </div>

    <div class="buttons color-theme-list">
        <button type="button" id="color-theme-preview-0" class="button size-3" data-theme="0">White</button>
        <button type="button" id="color-theme-preview-1" class="button size-3" data-theme="1">Sepia</button>
        <button type="button" id="color-theme-preview-2" class="button size-3" data-theme="2">Night</button>
    </div>
</div>

    </div>

    <!-- Actions Right -->
    
    <div class="dropdown pull-right">
        <a href="#" class="btn toggle-dropdown" aria-label="Toggle share dropdown"><i class="fa fa-share-alt"></i>
        </a>
        <div class="dropdown-menu font-settings dropdown-left">
            <div class="dropdown-caret">
                <span class="caret-outer"></span>
                <span class="caret-inner"></span>
            </div>
            <div class="buttons">
                <button type="button" data-sharing="twitter" class="button">Twitter</button>
                <button type="button" data-sharing="google-plus" class="button">Google</button>
                <button type="button" data-sharing="facebook" class="button">Facebook</button>
                <button type="button" data-sharing="weibo" class="button">Weibo</button>
                <button type="button" data-sharing="instapaper" class="button">Instapaper</button>
            </div>
        </div>
    </div>
    

    
    <a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Share on Google Plus"><i class="fa fa-google-plus"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Share on Facebook"><i class="fa fa-facebook"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Share on Twitter"><i class="fa fa-twitter"></i></a>
    
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >OC中的私有方法</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-gitbook_359">
                    
                        <h1 id="sel">SEL类型</h1>
<h2 id="">本小节知识:</h2>
<ol>
<li>【理解】什么是SEL类型</li>
<li>【掌握】SEL使用</li>
<li>【理解】OC方法查找顺序</li>
</ol>
<hr>
<h2 id="1sel">1.什么是SEL类型</h2>
<ul>
<li><p>SEL类型代表着方法的签名，在类对象的方法列表中存储着该签名与方法代码的对应关系</p>
</li>
<li><p>每个类的方法列表都存储在类对象中</p>
</li>
<li>每个方法都有一个与之对应的SEL类型的对象</li>
<li>根据一个SEL对象就可以找到方法的地址，进而调用方法</li>
<li><p>SEL类型的定义</p>
<ul>
<li>typedef struct objc_selector     *SEL;</li>
</ul>
</li>
<li><p>首先把test这个方法名包装成sel类型的数据</p>
</li>
<li>根据SEL数据到该类的类对象中，去找对应的方法的代码，如果找到了就执行该代码</li>
<li>如果没有找到根据类对象上的父类的类对象指针，去父类的类对象中查找，如果找到了，则执行父类的代码</li>
<li>如果没有找到，一直像上找，直到基类(NSObject)</li>
<li><p>如果都没有找到就报错。</p>
</li>
<li><blockquote>
<p>注意：</p>
<ul>
<li>在这个操作过程中有缓存,第一次找的时候是一个一个的找，非常耗性能，之后再用到的时候就直接使用。</li>
</ul>
</blockquote>
</li>
</ul>
<pre><code class="lang-objc">Dog *dog=[[Dog alloc] init];
[dog eat];
</code></pre>
<h2 id="imagesa13selpng"><img src="../images/a13/sel.png" alt=""></h2>
<h2 id="2sel">2.SEL使用</h2>
<ul>
<li><p>定义普通的变量</p>
<ul>
<li>如：SEL sel = @selector(show);</li>
</ul>
</li>
<li><p>作为方法实参与NSObject配合使用</p>
</li>
<li>检验对象是否实现了某个方法<ul>
<li>- (BOOL) respondsToSelector: (SEL)selector 判断实例是否实现这样方法</li>
<li>+ (BOOL)instancesRespondToSelector:(SEL)aSelector;</li>
</ul>
</li>
</ul>
<pre><code class="lang-objc">    <span class="hljs-built_in">BOOL</span> flag;
    <span class="hljs-comment">// [类 respondsToSelector]用于判断是否包含某个类方法</span>
    flag = [Person respondsToSelector:<span class="hljs-keyword">@selector</span>(objectFun)]; <span class="hljs-comment">//NO</span>
    flag = [Person respondsToSelector:<span class="hljs-keyword">@selector</span>(classFun)]; <span class="hljs-comment">//YES</span>

    Person *obj = [[Person alloc] init];

    <span class="hljs-comment">// [对象 respondsToSelector]用于判断是否包含某个对象方法</span>
    flag = [obj respondsToSelector:<span class="hljs-keyword">@selector</span>(objectFun)]; <span class="hljs-comment">//YES</span>
    flag = [obj respondsToSelector:<span class="hljs-keyword">@selector</span>(classFun)]; <span class="hljs-comment">//NO</span>

    <span class="hljs-comment">// [类名 instancesRespondToSelector]用于判断是否包含某个对象方法</span>
    <span class="hljs-comment">// instancesRespondToSelectorr只能写在类名后面, 等价于 [对象 respondsToSelector]</span>
    flag = [Person instancesRespondToSelector:<span class="hljs-keyword">@selector</span>(objectFun)]; <span class="hljs-comment">//YES</span>
    flag = [Person instancesRespondToSelector:<span class="hljs-keyword">@selector</span>(classFun)]; <span class="hljs-comment">//NO</span>
</code></pre>
<ul>
<li>让对象执行某个方法<ul>
<li>- (id)performSelector:(SEL)aSelector;</li>
<li>- (id)performSelector:(SEL)aSelector withObject:(id)object;</li>
<li>- (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2;</li>
</ul>
</li>
</ul>
<pre><code class="lang-objc">    Person *p = [Person new];
    SEL s1 = <span class="hljs-keyword">@selector</span>(objectFun);
    [p performSelector:s1];

    SEL s2 = <span class="hljs-keyword">@selector</span>(objectFun:);
    [p performSelector:s2 withObject:<span class="hljs-string">@"lnj"</span>];

    SEL s3 = <span class="hljs-keyword">@selector</span>(objectFun:value2:);
    [p performSelector:s3 withObject:<span class="hljs-string">@"lnj"</span> withObject:<span class="hljs-string">@"lmj"</span>];

    SEL s4 = <span class="hljs-keyword">@selector</span>(classFun);
    [Person performSelector:s4];

    SEL s5 = <span class="hljs-keyword">@selector</span>(classFun:);
    [Person performSelector:s5 withObject:<span class="hljs-string">@"lnj"</span>];

    SEL s6 = <span class="hljs-keyword">@selector</span>(classFun:value2:);
    [Person performSelector:s6 withObject:<span class="hljs-string">@"lnj"</span> withObject:<span class="hljs-string">@"lmj"</span>];
</code></pre>
<ul>
<li>作为方法形参</li>
</ul>
<pre><code class="lang-objc"><span class="hljs-class"><span class="hljs-keyword">@implementation</span> <span class="hljs-title">Person</span></span>

- (<span class="hljs-keyword">void</span>)makeObject:(<span class="hljs-keyword">id</span>) obj performSelector:(SEL) selector
{
    [obj performSelector:selector];
}
<span class="hljs-keyword">@end</span>

<span class="hljs-keyword">int</span> main(<span class="hljs-keyword">int</span> argc, <span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span> * argv[]) {

    Person *p = [Person new];
    SEL s1 = <span class="hljs-keyword">@selector</span>(eat);
    Dog *d = [Dog new];
    [p makeObject:d performSelector:s1];

    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<hr>
<h2 id="3oc">3.OC方法查找顺序</h2>
<p><img src="../images/a13/sldxldxydx.png" alt=""></p>
<ul>
<li><p>1.给实例对象发送消息的过程(调用对象方法)</p>
<ul>
<li>根据对象的isA指针去该对象的类方法中查找，如果找到了就执行</li>
<li>如果没有找到，就去该类的父类类对象中查找</li>
<li>如果没有找到就一直往上找，直到跟类（NSObject）</li>
<li>如果都没有找到就报错</li>
</ul>
</li>
<li><p>2.给类对象发送消息(调用类方法)</p>
<ul>
<li>根据类对象的isA指针去元对象中查找，如果找到了就执行</li>
<li>如果没有找到就去父元对象中查找</li>
<li>如果如果没有找到就一直往上查找，直到根类（NSOject）</li>
<li>如果都没有找到就报错</li>
</ul>
</li>
</ul>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../14.html" class="navigation navigation-prev navigation-unique" aria-label="Previous page: 类的启动过程"><i class="fa fa-angle-left"></i></a>
        
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="https://cdn.mathjax.org/mathjax/2.4-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-mathjax/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontSettings":{"theme":null,"family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
